package ysq.xyg.sys.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.sys.dao.SysDictContentDao;
import ysq.xyg.sys.model.po.SysDictContent;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.sys.model.vo.SysDictContentVo;
import ysq.xyg.sys.service.SysDictContentService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 系统部分-字典service实现
 *
 * @author shengQiang yu
 * @date 2018-03-18 15:42
 */
@Service("baseDictContentService")
public class SysDictContentServiceImpl extends BaseServiceImpl<SysDictContent> implements SysDictContentService {

    @Resource
    private SysDictContentDao sysDictContentDao;

    private SysDictContent sysDictContent;

    @Autowired
    public void setBaseDao(SysDictContentDao sysDictContentDao) {
        this.baseDao = sysDictContentDao;
    }


    /**
     * 分页查询
     *
     * @param sysDictContentVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2018-03-18 16:26
     */
    public ReturnEntity page(SysDictContentVo sysDictContentVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" sort desc,update_time desc");
        List<SysDictContent> list = acceptList(sysDictContentVo);
        PageInfo<SysDictContent> page=new PageInfo<SysDictContent>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param sysDictContentVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2018-03-18 16:26
     */
    public ReturnEntity addOrUpdate(SysDictContentVo sysDictContentVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(sysDictContentVo.getId())){
            sysDictContent=new SysDictContent(currentUser);
            isUpate=false;
        }else{
            sysDictContent=selectByPrimaryKey(sysDictContentVo.getId());
            if(ObjectUtil.isNull(sysDictContent)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            sysDictContent.setUpdateTime(new Date());
            sysDictContent.setUpdateUser(currentUser.getId());
        }
        sysDictContent.setName(sysDictContentVo.getName());
        sysDictContent.setSort(sysDictContentVo.getSort());
        sysDictContent.setValue(sysDictContentVo.getValue());
        sysDictContent.setDictType(sysDictContentVo.getDictType());
        sysDictContent.setStatus(sysDictContentVo.getStatus());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(sysDictContent);
        }else{
            num=insert(sysDictContent);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id删除
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2018-03-18 16:26
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        SysDictContent sysDictContent=selectByPrimaryKey(id);
        if(sysDictContent!=null){
            sysDictContent.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            int num=updateByPrimaryKey(sysDictContent);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 根据dictType获取集合
     *
     * @param dictTypes dictType集合,多个以“，”隔开
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2018-03-18 16:26
     */
    public ReturnEntity listByDictTypes(String dictTypes) {
        String[] dictTyps =dictTypes.split(",");
        List<SysDictContent> lists=new ArrayList<SysDictContent>();
        for(String dictType :dictTyps){
            List<SysDictContent> list=acceptList(new SysDictContentVo(dictType,BaseConstant.BASE_STATUS_NORMAL));
            lists.addAll(list);
        }
        if (lists != null && lists.size()>0) {
            return new ReturnEntity(lists);
        }else{
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
    }
    /**
     * 根据dictType获取集合,并筛选
     *
     * @param dictType dictType集合
     * @param values 值集合,多个以“，”隔开
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2018-03-18 16:26
     */
    public ReturnEntity listByDictTypeByValues(String dictType,String values) {
        List<SysDictContent> lists=new ArrayList<SysDictContent>();
        List<SysDictContent> list=acceptList(new SysDictContentVo(dictType,BaseConstant.BASE_STATUS_NORMAL));
        if(StrUtil.isNotEmpty(values)&&list.size()>0){
            String[] valuesList=values.split(",");
            for(SysDictContent sysDictContent:list){
                for(String v:valuesList){
                    if(v.equals(sysDictContent.getValue())){
                        lists.add(sysDictContent);
                        break;
                    }
                }
            }
        }else{
            lists=list;
        }
        if (lists != null && lists.size()>0) {
            return new ReturnEntity(lists);
        }else{
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
    }
    /**
     * 根据dictType获取移动端下拉选项
     *
     * @param dictType dictType对象
     * @return JSONArray 对象
     * @author shengQiang yu
     * @date 2018-03-18 16:26
     */
    public JSONArray accpetDictSelect(String dictType) {
        List<SysDictContent> list=acceptList(new SysDictContentVo(dictType,BaseConstant.BASE_STATUS_NORMAL));
        JSONArray jsonArray=new JSONArray();
        JSONObject jsonObject=null;
        if (list != null && list.size()>0) {
            for (SysDictContent sysDictContent : list) {
                jsonObject = new JSONObject();
                jsonObject.put("value", sysDictContent.getValue());
                jsonObject.put("title", sysDictContent.getName());
                jsonArray.add(jsonObject);
            }
        }
        return jsonArray;
    }
    /**
     * 根据value获取对象值
     *
     * @param value dictType对象值
     * @param sysDictContentList dictType对象集合
     * @return JSONArray 对象
     * @author shengQiang yu
     * @date 2018-03-18 16:26
     */
    public SysDictContent accpetDictContent(String value,List<SysDictContent> sysDictContentList) {
        if (sysDictContentList != null && sysDictContentList.size()>0) {
            for (SysDictContent sysDictContent : sysDictContentList) {
                if(sysDictContent.getValue().equals(value)){
                    return sysDictContent;
                }
            }
        }
        return null;
    }
    /**
     * 获取集合
     *
     * @param sysDictContentVo 对象
     * @return List<BaseDictContent> BaseDictContent集合
     * @author shengQiang yu
     * @date 2019-03-18 16:20
     */
    public List<SysDictContent> acceptList(SysDictContentVo sysDictContentVo){
        Example example = new Example(SysDictContent.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(sysDictContentVo.getName())){
            criteria.andLike("name","%" + sysDictContentVo.getName() + "%");
        }
        if(StringUtil.isNotEmpty(sysDictContentVo.getDictType())){
            criteria.andEqualTo("dictType",sysDictContentVo.getDictType());
        }
        if(StringUtil.isNotEmpty(sysDictContentVo.getValue())){
            criteria.andEqualTo("value",sysDictContentVo.getValue());
        }
        if(StringUtil.isNotEmpty(sysDictContentVo.getStatus())){
            criteria.andEqualTo("status",sysDictContentVo.getStatus());
        }
        if(StringUtil.isNotEmpty(sysDictContentVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",sysDictContentVo.getRemoveTag());
        }
        example.setOrderByClause(" sort desc,update_time desc");
        return sysDictContentDao.selectByExample(example);
    }
}
